// 搜索商品
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noMore:false,
    // 分类
    classify:[],
    priceMap:[
      {
        map:"60元以下",
        checked:false
      },
      {
        map: "60~150元",
        checked: false
      },
      {
        map: "150元以上",
        checked: false
      }
    ],
    // 当前选择的筛选方式（综合，销售，筛选）
    checked:"1",
    // 显示筛选选择框
    filter:false,
    //综合选择框的显示
    choose:false,
    shop_id:"",
    product: [],
    // 搜索时的title，输入的文字
    title:"",
    // 升序2或者降序1，空是综合
    order: "",
    // 选择的分类
    cate_id: "",
    start_price: "",
    end_price: "",
    p:1,
    // 滚动至顶部
    scrollTop:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shop_id = app.globalData.shop_id;
    this.setData({
      shop_id:shop_id
    })
    this.search()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var shop_id = app.globalData.shop_id;
      wx.request({
        url: app.globalData.yuming + 'merchant.php/Api/Category/cateList',
        data:{
          shop_id:shop_id
        },
        header:{
          "content-type":"application/json"
        },
        success:function(res){
          var classify = res.data.data;
          this.setData({
            classify:classify
          })
        }.bind(this)
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var shop_id = app.globalData.shop_id;
    this.setData({
      shop_id: shop_id
    })
    this.search()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.name,
      path: "/pages/shouye/shouye",
      imageUrl: app.globalData.headPic
    }
  },
  /**
   * 改变input搜索条件
   */
  changeCondition:function(e){
    var condition = e.detail.value
    this.setData({
      title:condition
    })
  },
  /**
   * 综合查询
   */
  show:function(){
    var choose = this.data.choose;
    this.setData({
      choose:!choose,
      checked:"1",
      noMore:false
    })
  },
  sales:function(){
    this.setData({
      checked: "2",
      choose:false,
      order:"",
      p:1,
      noMore: false
    })
    this.search()
  },
  shaixuan:function(){
    this.setData({
      checked:"3",
      choose: false,
      filter:true,
      noMore: false
    })
  },
  /**
   * 筛选模块
   */
  close:function(){
    var priceMap = this.data.priceMap;
    var classify = this.data.classify;
    priceMap.map(function(val){
      val.checked = false
    });
    classify.map(function (val) {
      val.checked = false
    })
    this.setData({
      filter:false,
      start_price:"",
      end_price:"",
      priceMap: priceMap,
      classify: classify,
      noMore: false
    })
  },
  complate:function(){
    this.setData({
      filter: false,
      p:1,
      noMore: false
    })
    this.search()
    
  },
  /**
   * 综合，降序，升序
   */
  changeOrderUp:function(){ 
      this.setData({
        order: 2,
        choose: false,
        p:1,
        noMore: false
      }) ;
      this.search();
  },
  changeOrderDown:function(){
    this.setData({
      order: 1,
      choose: false,
      p:1,
      noMore: false
    });
    this.search();
  },
  changeOrder:function(){
    this.setData({
      order: "",
      choose: false,
      p:1,
      noMore: false
    });
    this.search();
  },
  /**
   * 请求搜索
   */
  search:function(){
    var title = this.data.title;
    var shop_id = this.data.shop_id;
    var cate_id = this.data.cate_id;
    var start_price = this.data.start_price;
    var end_price = this.data.end_price;
    var order = this.data.order;
    var p =this.data.p;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Product/productList',
      data:{
        shop_id: shop_id,
        title: title,
        cate_id: cate_id,
        start_price: start_price,
        end_price: end_price,
        order: order,
        p: p,
        scrollTop:0
      },
      header: {
        "content-type":"application/x-www-form-urlencoded"
      },
      success:function(res){
        var product = res.data.data;
        this.setData({
          product:product
        })
      }.bind(this)
    })
  },
  /**
   * 低价格输入框聚焦时
   */
  focusLow:function(){
    var priceMap = this.data.priceMap;
    priceMap.map(function(val){
      val.checked = false
    })
      var start_price = "";
      this.setData({
        priceMap: priceMap,
        start_price: start_price
      })
  },
  /**
   * 高价格输入框聚焦时
   */
  focusHigh: function () {
    var priceMap = this.data.priceMap;
    priceMap.map(function (val) {
      val.checked = false
    })
    var end_price = "";
    this.setData({
      priceMap: priceMap,
      end_price: end_price
    })
  },
  /**
   * 输入低价格
   */
  low:function(e){
    var start_price = e.detail.value;
    this.setData({
      start_price: start_price
    })
  },
  /**
   * 输入高价格
   */
  high: function (e) {
    var end_price = e.detail.value;
    this.setData({
      end_price: end_price
    })
  },
  /**
   * 选择分类
   */
  cate:function(e){
    var idx = e.currentTarget.dataset.idx;
    var cate_id = idx;
    var classify = this.data.classify;
    classify.map(function(val){
      if(val.cate_id == idx){
        if(val.checked == true){
          cate_id = ""
        }
        val.checked = !val.checked;
        
      }else{
        val.checked = false;
      }
    })
    this.setData({
      cate_id: cate_id,
      classify: classify
    })
  },
  /**
   * 选择价格区间
   */
  choosePrice:function(e){
    var idx = e.currentTarget.dataset.idx;
    var priceMap = this.data.priceMap;
    if(priceMap[idx].checked == true){
      priceMap[idx].checked = false;
      var end_price = "";
      var start_price = "";
      this.setData({
        end_price: end_price,
        start_price: start_price,
        priceMap: priceMap
      })
    }else{
      priceMap.map(function (val) {
        val.checked = false;
      })
      priceMap[idx].checked = true;
      if (idx == 0) {
        var end_price = 60;
        var start_price = "";
        this.setData({
          end_price: end_price,
          start_price: start_price,
          priceMap: priceMap
        })
      } else if (idx == 1) {
        var start_price = 60;
        var end_price = 150;
        this.setData({
          end_price: end_price,
          start_price: start_price,
          priceMap: priceMap
        })
      } else if (idx == 2) {
        var start_price = 150;
        var end_price = "";
        this.setData({
          end_price: end_price,
          start_price: start_price,
          priceMap: priceMap
        })
      }
    }
    
  },
  /**
   * 分页
   */
  most:function(){
    var noMore = this.data.noMore;
    if(noMore == false){
    var p = this.data.p;
    p = p + 1;
    var product = this.data.product;
    var title = this.data.title;
    var shop_id = this.data.shop_id;
    var cate_id = this.data.cate_id;
    var start_price = this.data.start_price;
    var end_price = this.data.end_price;
    var order = this.data.order;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Product/productList',
      data: {
        shop_id: shop_id,
        title: title,
        cate_id: cate_id,
        start_price: start_price,
        end_price: end_price,
        order: order,
        p: p
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        var pro = res.data.data;
        if(pro !=""){
          product.push(pro)
        }else{
          this.setData({
            noMore:true
          })
        }
        this.setData({
          product: product
        })
      }.bind(this)
    })
  }
  }
})